package com.senior.sendmessage.xxl;

import com.senior.sendmessage.service.SendService;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.StopWatch;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RestController;

import javax.mail.MessagingException;
import java.util.concurrent.TimeUnit;

/**
 * controller
 *
 * @author bjl_m
 */
@Component
@RestController
@RequiredArgsConstructor
@Slf4j
public class MailScheduleXxlJob {
    private final SendService sendService;


    @XxlJob(value = "sendMailHandler", init = "init", destroy = "destroy")
    public void sendMail() throws MessagingException {
        final StopWatch started = StopWatch.createStarted();
        XxlJobHelper.log("定时任务开始！");
        sendService.sendMail();
        XxlJobHelper.log("定时任务结束！");
        log.info("发送邮件耗时：【{}】秒", started.getTime(TimeUnit.SECONDS));
    }

    public void init() {
        log.info("init");
    }

    public void destroy() {
        log.info("destroy");
    }

}
